/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserFrom.java
 *
 * Created on Oct 18, 2011, 1:03:48 AM
 */
package project;

import Utils.Helper;
import Utils.JDBCHelper;
import Utils.checkValidate;
import beanControl.userControl;
import beanInfo.users;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

/**
 *
 * @author lyan
 */
public class UserFrom extends javax.swing.JFrame {

    userControl use = new userControl();
    users us = new users();
    String id = " ";

    /** Creates new form UserFrom */
    public UserFrom(String ID) {
        initComponents();
        this.id = ID;
        //select user by id
//System.out.println(Admin.cruUser);
        display();


    }

    public users SelectUser() {
        users us = new users();
        try {

            String sql = "select * from Users where UserName='" + id + "' ";
            ResultSet rs = JDBCHelper.excuteSelect(sql);
            while (rs.next()) {
                us.setUserName(rs.getString("UserName"));
                us.setName(rs.getString("Name"));
                us.setEmail(rs.getString("Email"));
                //  us.setBirthday(rs.getString("Brithday"));
                us.setAddress(rs.getString("Address"));
                us.setPhone(rs.getString("phone"));


            }
        } catch (SQLException ex) {
            Logger.getLogger(UserFrom.class.getName()).log(Level.SEVERE, null, ex);
        }
        return us;
    }

    private void display() {

        if (Admin.cruUser == 0) {
            txtAdrress.setText("");
            txtEmail.setText("");
            txtPhone.setText("");
            txtUserName.setText("");
            txtPass.setText("");
        } else {
            users u = SelectUser();
            txtUserName.setText(u.getUserName());
            txtName.setText(u.getName());
            txtEmail.setText(u.getEmail());
            //   Helper.ConvertDateToString(dbBirthday.setDate(u.getBirthday().toString()));
            txtAdrress.setText(u.getAddress());
            txtPhone.setText(u.getPhone());
        }
    }

    public void insertUsers() {
        us.setUserName(txtUserName.getText());
        us.setPassWord(txtPass.getText());
        us.setName(txtName.getText());
        us.setBirthday(Helper.ConvertDateToString(dbBirthday.getDate()));
        us.setEmail(txtEmail.getText());
        us.setAddress(txtAdrress.getText());

        if (rdbNam.isSelected() == true) {
            us.setGender("Male");
        } else {
            us.setGender("Female");
        }
        us.setRole(cboRole.getSelectedItem().toString());
        us.setPhone(txtPhone.getText());
        us.setStatus("No");
        use.insertUser(us);

        txtAdrress.setText("");
        txtEmail.setText("");
        txtName.setText("");
        txtPhone.setText("");
        txtUserName.setText("");

    }

    public void UpdateUser() {

        us.setUserName(txtUserName.getText());
        //us.setPassWord(txtPass.getText());
        us.setName(txtName.getText());
        us.setBirthday(Helper.ConvertDateToString(dbBirthday.getDate()));
        us.setEmail(txtEmail.getText());
        us.setAddress(txtAdrress.getText());
        us.setRole(cboRole.getSelectedItem().toString());
        us.setPhone(txtPhone.getText());
        us.setStatus("No");
        use.updateUser(us);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtAdrress = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        cboRole = new javax.swing.JComboBox();
        rdbNam = new javax.swing.JRadioButton();
        rdbNu = new javax.swing.JRadioButton();
        btnReset1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtPass = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        dbBirthday = new com.toedter.calendar.JDateChooser();
        btnUpdate = new javax.swing.JButton();

        jPanel1.setBackground(new java.awt.Color(0, 102, 102));

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setLayout(null);

        jLabel4.setText("UserName");
        jPanel3.add(jLabel4);
        jLabel4.setBounds(50, 80, 70, 20);

        jLabel5.setText("Password");
        jPanel3.add(jLabel5);
        jLabel5.setBounds(50, 120, 70, 20);
        jPanel3.add(txtUserName);
        txtUserName.setBounds(140, 70, 229, 30);

        jLabel7.setText("Email");
        jPanel3.add(jLabel7);
        jLabel7.setBounds(60, 220, 50, 20);
        jPanel3.add(txtEmail);
        txtEmail.setBounds(140, 210, 230, 30);

        jLabel8.setText("Address");
        jPanel3.add(jLabel8);
        jLabel8.setBounds(50, 310, 50, 20);
        jPanel3.add(txtAdrress);
        txtAdrress.setBounds(140, 300, 235, 30);

        jLabel9.setText("Role");
        jPanel3.add(jLabel9);
        jLabel9.setBounds(50, 390, 50, 20);

        jLabel10.setText("Genden");
        jPanel3.add(jLabel10);
        jLabel10.setBounds(50, 340, 60, 20);

        jLabel11.setText("Phone");
        jPanel3.add(jLabel11);
        jLabel11.setBounds(50, 440, 50, 20);
        jPanel3.add(txtPhone);
        txtPhone.setBounds(140, 430, 230, 30);

        btnAdd.setBackground(new java.awt.Color(0, 102, 102));
        btnAdd.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnAdd.setForeground(new java.awt.Color(255, 255, 255));
        btnAdd.setText("Insert");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel3.add(btnAdd);
        btnAdd.setBounds(120, 470, 80, 30);

        cboRole.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2" }));
        jPanel3.add(cboRole);
        cboRole.setBounds(140, 380, 230, 30);

        buttonGroup1.add(rdbNam);
        rdbNam.setText("Male");
        jPanel3.add(rdbNam);
        rdbNam.setBounds(140, 340, 60, 23);

        buttonGroup1.add(rdbNu);
        rdbNu.setText("Female");
        jPanel3.add(rdbNu);
        rdbNu.setBounds(200, 340, 59, 23);

        btnReset1.setBackground(new java.awt.Color(0, 102, 102));
        btnReset1.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnReset1.setForeground(new java.awt.Color(255, 255, 255));
        btnReset1.setText("Cancel");
        btnReset1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReset1ActionPerformed(evt);
            }
        });
        jPanel3.add(btnReset1);
        btnReset1.setBounds(300, 470, 80, 30);

        jLabel2.setText("Name");
        jPanel3.add(jLabel2);
        jLabel2.setBounds(50, 170, 40, 20);
        jPanel3.add(txtName);
        txtName.setBounds(140, 160, 230, 30);
        jPanel3.add(txtPass);
        txtPass.setBounds(140, 110, 230, 30);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36));
        jLabel1.setForeground(new java.awt.Color(204, 0, 0));
        jLabel1.setText("USERS");
        jPanel3.add(jLabel1);
        jLabel1.setBounds(140, 20, 140, 40);

        jLabel3.setText("Birthday");
        jPanel3.add(jLabel3);
        jLabel3.setBounds(50, 264, 60, 20);

        dbBirthday.setDateFormatString("MM/dd/yyyy");
        jPanel3.add(dbBirthday);
        dbBirthday.setBounds(140, 250, 230, 30);

        btnUpdate.setBackground(new java.awt.Color(0, 102, 102));
        btnUpdate.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnUpdate.setForeground(new java.awt.Color(255, 255, 255));
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });
        jPanel3.add(btnUpdate);
        btnUpdate.setBounds(210, 470, 83, 30);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 534, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 458, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
// TODO add your handling code here:
    
    if (txtUserName.getText().length() == 0) {
        Helper.successMessage("Enter userName ", "Thong bao");
    } else if (txtPass.getText().length() == 0) {
        Helper.successMessage("Enter password  ", "wanning");
    } else if (txtAdrress.getText().length() == 0) {
        Helper.successMessage("Enter Address ", "wanning");

    } else if (txtEmail.getText().length() == 0) {
        Helper.successMessage(" Enter Email ", "wanning");

    } else if (txtName.getText().length() == 0) {
        Helper.successMessage("Enter Name ", "wanning");
    } else if (txtPhone.getText().length() == 0) {
        Helper.successMessage("Enter phone ", "wanning");
    } else if (checkValidate.isEmail(txtEmail.getText()) == false) {
        Helper.successMessage("Email Invalid", "wanning");
    } else if (checkValidate.checkPhone(txtPhone.getText()) == false) {
        Helper.successMessage("Phone is number (9-15)", "wanning");

    } else if (Helper.isValidDate(Helper.ConvertDateToString(dbBirthday.getDate())) == false) {
        Helper.successMessage("birthday invalid", " wanning");
    }
      
    
    else {
        insertUsers();
    }

}//GEN-LAST:event_btnAddActionPerformed

private void btnReset1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReset1ActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_btnReset1ActionPerformed

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
// TODO add your handling code here:
    if (txtUserName.getText().length() == 0) {
        Helper.successMessage("Enter userName ", "Thong bao");
    
    } else if (txtAdrress.getText().length() == 0) {
        Helper.successMessage("Enter Address ", "wanning");

    } else if (txtEmail.getText().length() == 0) {
        Helper.successMessage(" Enter Email ", "wanning");

    } else if (txtName.getText().length() == 0) {
        Helper.successMessage("Enter Name ", "wanning");
    } else if (txtPhone.getText().length() == 0) {
        Helper.successMessage("Enter phone ", "wanning");
    } else if (checkValidate.isEmail(txtEmail.getText()) == false) {
        Helper.successMessage("Email Invalid", "wanning");
    } else if (checkValidate.checkPhone(txtPhone.getText()) == false) {
        Helper.successMessage("Phone is number (9-15)", "wanning");

//    } else if (Helper.isValidDate(Helper.ConvertDateToString(dbBirthday.getDate())) == false) {
//        Helper.successMessage("birthday invalid", " wanning");
    } else {
        UpdateUser();
    }
}//GEN-LAST:event_btnUpdateActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(UserFrom.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(UserFrom.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(UserFrom.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(UserFrom.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new UserFrom().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnReset1;
    private javax.swing.JButton btnUpdate;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cboRole;
    private com.toedter.calendar.JDateChooser dbBirthday;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton rdbNam;
    private javax.swing.JRadioButton rdbNu;
    private javax.swing.JTextField txtAdrress;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtName;
    private javax.swing.JPasswordField txtPass;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
